const CalendarHandler = {
	currentYear: 0,
	currentMonth: 0,
	currentDay:new Date().getDate(),
	isRunning: false,
	showYearStart:2009,
	tag:0,
	initialize: function() {
		this.CreateCalendar(0, 0, 0);
		this.currentDay = new Date().getDate();
	},
	IsRuiYear: function(aDate) {
		return (0 == aDate % 4 && (aDate % 100 != 0 || aDate % 400 == 0));
	},
	CalculateMonthDays: function(m, y) {
		var mDay = 0;
		if (m == 0 || m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
			mDay = 31;
		} else {
			if (m == 2) {
				//判断是否为芮年
				var isRn = this.IsRuiYear(y);
				if (isRn == true) {
					mDay = 29;
				} else {
					mDay = 28;
				}
			} else {
				mDay = 30;
			}
		}
		return mDay;
	},
	CreateCalendar: function(y, m, d) {
		//$dayItem = $("<div class=\"dayItem\"></div>");
		//获取当前月份的天数
		var nowDate = new Date();
		if(y==nowDate.getFullYear()&&m==nowDate.getMonth()+1||(y==0&&m==0))
		$(".currentDay").show();
		var nowYear = y == 0 ? nowDate.getFullYear() : y;
		this.currentYear = nowYear;
		var nowMonth = m == 0 ? nowDate.getMonth() + 1 : m;
		this.currentMonth = nowMonth;
		var nowDay = d == 0 ? nowDate.getDate() : d;
		$(".selectYear").html(nowYear + "年");
		$(".selectMonth").html(nowMonth + "月");
		$(".selectDay").html(nowDay + "日");
	},
	CalculateNextDays: function() {
		if (this.isRunning == false) {
			$(".currentDay").show();
			var m = this.currentMonth;
			var y = this.currentYear;
			//var nowDate = new Date();
			var d = 1;
			if (m == 0 || m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
				if(this.currentDay > 30){
					this.currentDay = 1;
					m = this.currentMonth == 12 ? 1 : this.currentMonth + 1;
					y = this.currentMonth == 12 ? (this.currentYear + 1) : this.currentYear;
				}else{
					d = ++this.currentDay;
				}
			} else {
				if (m == 2) {
					//判断是否为芮年
					var isRn = this.IsRuiYear(y);
					if (isRn == true) {
						if(this.currentDay > 28){
							this.currentDay = 1;
							m = this.currentMonth + 1;
						}else{
							d = ++this.currentDay;
						}
					} else {
						if(this.currentDay > 27){
							this.currentDay = 1;
							m = this.currentMonth + 1;
						}else{
							d = ++this.currentDay;
						}
					}
				} else {
					if(this.currentDay > 29){
						this.currentDay = 1;
						m = this.currentMonth + 1;
					}else{
						d = ++this.currentDay;
					}
				}
			}
			const $calendarItem = this.CreateCalendar(y, m, d);
		}
	},
	CalculateLastDays: function() {
		if (this.isRunning == false) {
			$(".currentDay").show();				
			var m = this.currentMonth;
			var y = this.currentYear;
			if(m == 0 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12){
				if(this.currentDay < 2){
					var d = 30;
					this.currentDay = 30;
					m = this.currentMonth == 1 ? 12 : this.currentMonth - 1;
				}else{
					d = --this.currentDay;
				}
			}else if(m == 3){
				//判断是否为芮年
				var isRn = this.IsRuiYear(y);
				if (isRn == true) {
					if(this.currentDay < 2){
						var d = 29;
						this.currentDay = 29;
						m = this.currentMonth == 1 ? 12 : this.currentMonth - 1;
					}else{
						d = --this.currentDay;
					}
				} else {
					if(this.currentDay < 2){
						var d = 28;
						this.currentDay = 28;
						m = this.currentMonth == 1 ? 12 : this.currentMonth - 1;
					}else{
						d = --this.currentDay;
					}
				}
			}
			else{
				if (m == 2  || m == 1){
					if(this.currentDay < 2){
						var d = 31;
						this.currentDay = 31;
						m = this.currentMonth == 1 ? 12 : this.currentMonth - 1;
						y = this.currentMonth == 1 ? (this.currentYear - 1) : this.currentYear;
					}else{
						d = --this.currentDay;
					}
				}else{
					if(this.currentDay < 2){
						var d = 31;
						this.currentDay = 31;
						m = this.currentMonth == 1 ? 12 : this.currentMonth - 1;
					}else{
						d = --this.currentDay;
					}
				}
			}

			const $calendarItem = this.CreateCalendar(y, m, d);
		}
	},
	CreateCurrentCalendar: function() {
		if (this.isRunning == false) {
			$(".currentDay").show();
			this.currentDay = new Date().getDate();
			const $calendarItem = this.CreateCalendar(0, 0, 0);
		}
	}
}

$(document).ready(function(){
	CalendarHandler.initialize();
})

export default CalendarHandler